import os, datetime
from flask import Blueprint, render_template, current_app, url_for
from flask_login import current_user, login_required
from FileShare.utils import sizeFormat

main_bp = Blueprint('main', __name__)

@main_bp.route('/')
@login_required
def index():
    file_path = os.path.join(current_app.config['UPLOAD_FOLDER'], current_user.username)
    filelists = []

    if os.path.isdir(file_path):
        for filename in os.listdir(file_path):
            file_object = {}
            file_path_uri = os.path.join(file_path, filename)
            time_local = datetime.datetime.fromtimestamp(os.path.getctime(file_path_uri))
            FILE_EXPIRE_DATE = time_local + datetime.timedelta(days=current_app.config['FILE_EXPIRE_DATE'])

            file_object['filename'] = filename
            file_object['fileSize'] = sizeFormat(os.path.getsize(file_path_uri))
            file_object['createData'] = time_local.strftime('%Y-%m-%d %H:%M:%S')
            file_object['ExpiredDate'] = FILE_EXPIRE_DATE.strftime('%Y-%m-%d %H:%M:%S')
            file_object['wget_url'] = url_for('files.download', _external=True ,filename='{}/{}'.format(current_user.username, filename))
            file_object['delete_url'] = url_for('files.delete', _external=True ,filename='{}/{}'.format(current_user.username, filename))
            filelists.append(file_object)
    return render_template('index.html', filelists=filelists)

@main_bp.route('/hello')
def hello():
    if current_user.is_authenticated:
        return '已经登陆了'
    else:
        return '还没有登陆'